﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Collections;

namespace monitor_server
{
    public delegate void sort_delegate(int a);

    public partial class Form6 : Form
    {        
        formenable d;
        static bool comp = false;
        int preColumn = 0;

        public Form6(formenable sender)
        {
            InitializeComponent();
            d = sender;
        }

        class ListViewItemComparer : IComparer
        {
            private int col;
            public ListViewItemComparer(int column)
            {
                if (column == 0)
                    col = 1;
                else
                    col = column;

            }
            public int Compare(object x, object y)
            {
                int ret = 0;
                DateTime dt1, dt2;
                string d1str = ((ListViewItem)x).SubItems[col].Text;
                string d2str = ((ListViewItem)y).SubItems[col].Text;
                //--------------------------Сортировка по столбцам, содержащим дату
                //if (DateTime.TryParseExact(d1str, "dd.MM/HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt1))
                /*if (DateTime.TryParse(d1str, out dt1))
                {
                    if (d2str == "Нет данных")
                    {
                        if (!comp)
                            dt2 = DateTime.ParseExact("01.01/00:00", "dd.MM/HH:mm", CultureInfo.InvariantCulture);
                        else
                            dt2 = DateTime.ParseExact("31.12/00:00", "dd.MM/HH:mm", CultureInfo.InvariantCulture);
                    }
                    else
                        //dt2 = DateTime.ParseExact(d2str, "dd.MM/HH:mm", CultureInfo.InvariantCulture);
                        dt2 = DateTime.Parse(d2str, CultureInfo.InvariantCulture);
                    ret = DateTime.Compare(dt1, dt2);
                }
                else if (d1str == "Нет данных")
                {
                    if (!comp)
                        dt1 = DateTime.ParseExact("01.01/00:00", "dd.MM/HH:mm", CultureInfo.InvariantCulture);
                    else
                        dt1 = DateTime.ParseExact("31.12/00:00", "dd.MM/HH:mm", CultureInfo.InvariantCulture);
                    if (d2str == "Нет данных")
                        dt2 = DateTime.ParseExact("01.01/00:00", "dd.MM/HH:mm", CultureInfo.InvariantCulture);
                    else
                        dt2 = DateTime.ParseExact(d2str, "dd.MM/HH:mm", CultureInfo.InvariantCulture);
                    ret = DateTime.Compare(dt1, dt2);
                }
                //--------------------------
                else
                {
                    //DateTime d1 = DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
                    //DateTime d2 = DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
                    ret = String.Compare(d1str, d2str);
                    //ret = DateTime.Compare(d1, d2);
                }
                 
                if (!comp)
                    ret *= -1;
                return ret;*/
                if (DateTime.TryParse(d1str, out dt1))
                {
                    if (DateTime.TryParse(d2str, out dt2))
                        ret = DateTime.Compare(dt1, dt2);
                    else
                        ret = -1;
                }
                else if (d1str == "Нет данных")
                {
                    ret = String.Compare(d1str, d2str);
                }
                else
                    ret = String.Compare(d1str, d2str);
                if (!comp)
                    ret *= -1;
                return ret;
            }
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            d(false);
            this.Close();
        }

        private void Form6_Shown(object sender, EventArgs e)
        {
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.Columns.Add("№");
            listView1.Columns[listView1.Columns.Count - 1].Width = 25;
            /*listView1.Columns.Add("Имя пользователя");
            listView1.Columns[listView1.Columns.Count - 1].Width = 150;
            listView1.Columns.Add("Группа");
            listView1.Columns[listView1.Columns.Count - 1].Width = 100;
            listView1.Columns.Add("Время входа");
            listView1.Columns[listView1.Columns.Count - 1].Width = 100;
            listView1.Columns.Add("Время выхода");
            listView1.Columns[listView1.Columns.Count - 1].Width = 100;*/
            listView1.Columns.Add("Имя пользователя/событие");
            listView1.Columns[listView1.Columns.Count - 1].Width = 200;
            listView1.Columns.Add("Детали");
            listView1.Columns[listView1.Columns.Count - 1].Width = 200;
            listView1.Columns.Add("Время начала работы");
            listView1.Columns[listView1.Columns.Count - 1].Width = 200;
            listView1.Columns.Add("Время завершения работы");
            listView1.Columns[listView1.Columns.Count - 1].Width = 200;
        }

        private void Form6_FormClosing(object sender, FormClosingEventArgs e)
        {
            d(false);
            //this.Close();
        }

        private void DataUpdate()
        {
            ColumnHeader[] colH = new ColumnHeader[listView1.Columns.Count];
            int ij = 0;
            foreach (ColumnHeader col in listView1.Columns)
            {
                colH[ij] = col;
                ij++;
            }
            listView1.Clear();
            listView1.Columns.AddRange(colH);
            int lvi_count = 1;
            int i = 0;
            //List<ListViewItem> list_str = new List<ListViewItem>();
            if (!DBOperations.IsConnected)
                DBOperations.DBConnect();
            string[] SelStr = DBOperations.EventJournalSelect();
            ListViewItem[] lvis = new ListViewItem[SelStr.Length];
            if (SelStr.Length == 1)
            {
                if (SelStr[0] == "err1")
                    MessageBox.Show("Не удалось установить соединение с БД.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }
            foreach (string str in SelStr)
            {
                ListViewItem lvi = new ListViewItem(lvi_count.ToString());
                string[] splitted = str.Split(';');
                for (i = 0; i < splitted.Length; i++)
                {
                    if (splitted[i] == "11.11.1111 11:11:11")
                        lvi.SubItems.Add("Нет данных");
                    else
                        lvi.SubItems.Add(splitted[i]);
                }
                //list_str.Add(lvi);
                lvis[lvi_count - 1] = lvi;
                lvi_count++;
            }
            //ListViewItem[] lvis = new ListViewItem[lvi_count - 1];
            /*i = 0;
            foreach (ListViewItem lv in list_str)
            {
                lvis[i] = lv;
                i++;
            }*/
            listView1.Items.AddRange(lvis);
            if (DBOperations.IsConnected)
                DBOperations.CloseConnect();
            //listView1.ListViewItemSorter = new ListViewItemComparer(3);
            sort_lv(3);
        }

        private void Form6_Load(object sender, EventArgs e)
        {
            DataUpdate();
        }

        private void обновитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataUpdate();
        }

        private void sort_lv(int a)
        {
            listView1.ListViewItemSorter = new ListViewItemComparer(a);
            for (int i = 0; i < listView1.Items.Count; i++)
                listView1.Items[i].Text = string.Concat(listView1.Items[i].Index + 1);
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == 0)
                return;
            if (!(preColumn != e.Column && !((e.Column == 0 || e.Column == 5) && (preColumn == 0 || preColumn == 5))))
                comp = !comp;
            preColumn = e.Column;
            sort_delegate sort = sort_lv;
            listView1.Invoke(sort, e.Column);
        }
    }
}
